package cn.gson.springmvc.model.service;

import cn.gson.springmvc.model.dao.YpcmedicalDao;
import cn.gson.springmvc.model.dao.YpcpatientDao;
import cn.gson.springmvc.model.mappers.YpcmedicalMapper;
import cn.gson.springmvc.model.pojos.Medical;
import cn.gson.springmvc.model.pojos.Patient;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;

@Service
@Transactional(rollbackFor = Exception.class)
public class YpcMedicalService {
    @Autowired
    YpcmedicalMapper ypcmedicalMapper;

    @Autowired
    YpcmedicalDao ypcmedicalDao;

    @Autowired
    YpcpatientDao ypcpatientDao;
    public JSONArray selectMedical(){
        return ypcmedicalMapper.selectMedical();
    }

    /**
     * 新增医疗卡
     * @param medical
     */
    @Transactional(value ="transactionManager" ,rollbackFor = Exception.class)
    public void addMedical(Medical medical,String patientName12){
        Patient patient = ypcmedicalMapper.selectPatient(patientName12);
        System.out.println(patient.getMedical().getMedicalId());

        Medical medical1 = ypcmedicalDao.save(medical);



    }

    /**
     * 充值
     * @param medicalId
     * @param medicalMoney
     */
    @Transactional(value ="transactionManager" ,rollbackFor = Exception.class)
    public void topUps(int medicalId, String medicalMoney){
        Medical medical=ypcmedicalDao.findById(medicalId).get();
        BigDecimal medicalBalance8 = medical.getMedicalBalance8();
        int a = Integer.parseInt(medicalMoney);
        int b=medicalBalance8.intValue();
        int c=a+b;
        BigDecimal d=new BigDecimal(BigInteger.valueOf(c));
        medical.setMedicalBalance8(d);
        ypcmedicalDao.save(medical);
    }

    /**
     * 医疗卡禁用/启用
     * @param medicalId
     */
    @Transactional(value="transactionManager",rollbackFor = Exception.class)
    public void updateStatus(int medicalId){
        //先根据编号查询用户
        Medical medical = ypcmedicalDao.findById(medicalId).get();

        System.out.println("后台数据"+medical.getMedicalState());
        if (medical.getMedicalState().equalsIgnoreCase("0")){
            medical.setMedicalState("1");
        }else{
            medical.setMedicalState("0");
        }
        ypcmedicalDao.save(medical);
    }

    @Transactional(value="transactionManager",rollbackFor = Exception.class)
    public void chongzhi(int medicalId){
        //先根据编号查询用户
        Medical medical = ypcmedicalDao.findById(medicalId).get();
        medical.setMedicalPass(123456);
    }

    @Transactional(value="transactionManager",rollbackFor = Exception.class)
    public JSONArray selectOne(String patientName12){
        System.out.println("前台数据"+patientName12);
        return ypcmedicalMapper.selectOne(patientName12);
    }

}
